'##################################################
'###  Top Level (Get root Path from system env)
'##################################################
function GetEnv(envVarName)
	On Error Resume Next
	' 创建Shell对象并获取值
	Set WshShell = CreateObject("WScript.Shell")
	envValue = WshShell.Environment("PROCESS")(envVarName) ' 优先查找进程级变量

	' 如果未找到则尝试系统级变量
	If Err.Number <> 0 Or envValue = "" Then
		Err.Clear
		envValue = WshShell.Environment("SYSTEM")(envVarName)
	End If

	' 如果仍未找到则尝试用户级变量
	If Err.Number <> 0 Or envValue = "" Then
		Err.Clear
		envValue = WshShell.Environment("USER")(envVarName)
	End If

	' 检查结果并输出
	If Err.Number = 0 And envValue <> "" Then
		GetEnv=envValue
	Else
		GetEnv=""
	End If
end function

'##################################################
'##################################################
'##################################################
sub IncludeVbs(filepath)
	Dim fso, file, content
	Set fso = CreateObject("Scripting.FileSystemObject")
	' 读取functions.vbs的内容
	Set file = fso.OpenTextFile(filepath, 1) ' 1表示只读
	content = file.ReadAll
	file.Close
	' 将functions.vbs的内容载入全局作用域
	ExecuteGlobal content
end sub

'##################################################
'##################################################
'##################################################
dim root,tmpfile
Dim doc
Set doc = application.ActiveDocument
dim dbDict:set dbDict = createobject("scripting.Dictionary")
dim fso:set fso = createobject("scripting.filesystemobject")	
dim color_def
main
set doc = nothing
set dbDict=nothing
set fso = nothing

sub main()	
	'// REUSE
	envVarName ="EXP_SHORTCUTS_ROOT"
	root = GetEnv(envVarName)'"C:\MentorGraphics\2005EXP\SDD_HOME\pvtk\env\"
	if root ="" then 
		msgbox "please set env value (EXP_SHORTCUTS_ROOT = ?)"
		exit sub
	end if
	
	call IncludeVbs(root & "\skill_lib.vbs")
	
	If ValidateServer(doc) = 0 Then
	    msgbox "Server validation failed."
	    exit sub
	End If	
	
	'// TODO
	color_def = root & "config\data\color_def.txt"
	'msgbox color_def
	
	dim pcbNets,pcbNet	
	Set pcbNets = doc.Nets(epcbSelectSelected)
	if pcbNets.count =0 then exit sub
	
	if args="" then
		args = InputBox ("set 0~39" ,args ,0 )
	end if

	dim ul
	ul = args

	initDict
	'msgbox dbDict.count
	dim selcolor
	Dim a,b,c,d,ischeck
	
	if dbDict.exists(ul)=false then
	else
		selcolor = dbDict.item(ul)	
		dim s,ls,i
		s = split(selcolor,",")
		ls = ubound(s)-lbound(s)+1
		if ls<2 then exit sub
		a = s(0)
		b = s(1)
		c = s(2)
		d = 1
		if ls>3 then d = s(3)
	end if
	
	dim colorPatternObj,colorPatternObj_erase
	set colorPatternObj = application.Utility.NewColorPattern(a, b, c,d)
	set colorPatternObj_erase= application.Utility.NewColorPattern(255, 255, 255,1)
	
	For each pcbNet In pcbNets
		If pcbNet.Selected = True Then
			pcbNetName = pcbNet.Name
			ischeck = doc.activeView.DisplayControl.Global.Net("True",pcbNet)
			'application.Gui.display ischeck
			set colorPatternObj = doc.activeView.DisplayControl.Global.NetColor("True",pcbNet)
			Dim aa,bb,cc,dd
			aa =colorPatternObj.red
			bb =colorPatternObj.blue
			cc =colorPatternObj.green
			colorPatternObj.red = a
			colorPatternObj.blue = b
			colorPatternObj.green =c
			colorPatternObj.index = d
			if (ul=0)then
				doc.activeView.DisplayControl.Global.Net("True",pcbNet)=false
				set doc.activeView.DisplayControl.Global.NetColor("True",pcbNet) = colorPatternObj_erase
			end if 
		
			if ul<>0 then
				if ischeck = false then
					doc.activeView.DisplayControl.Global.Net("True",pcbNet)=True
				end if
				'msgbox aa & "  " & bb & "  " & cc & chr(13) & chr(10) & a & "  " & b & "  " & c				 
				if aa<>a or bb<>b or cc<>c then 
					'msgbox "here"
					set doc.activeView.DisplayControl.Global.NetColor("True",pcbNet) = colorPatternObj
				end if
			End if
		end if
	next	
end sub

sub initDict()
	dim okey,oval
	dim fo:set fo = fso.opentextfile(color_def)
	dim freadall
	freadall = fo.readall()
	fo.close
	set fo = nothing
	
	freadall = replace(freadall,chr(13),"")
	dim s,ls,i
	s = split(freadall,chr(10))
	ls = ubound(s)-lbound(s)+1
	
	dim selitem
	dim cs,lcs,j
	dim cs2,lcs2,k
	for i=0 to ls-1
		selitem = trim(s(i))
		if selitem<>"" then
			cs = split(selitem,chr(9))
			lcs = ubound(cs)-lbound(cs)+1
			if lcs = 2 then
				okey = cs(0)
				oval = cs(1)			
				if dbDict.exists(okey)=true then
					dbDict.item(okey)=oval
				else
					call dbDict.add(okey,oval)
				end if	
			end if
		end if
	next
end sub